package com.cuit.controller;

import com.cuit.doamin.User;
import com.cuit.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 修改密码
     *
     * @param password
     * @param request
     * @return
     */
    @RequestMapping("/updatePwd")
    public ModelAndView updatePwd(String password, HttpServletRequest request) {

        ModelAndView mv = new ModelAndView();
        User user = (User) request.getSession().getAttribute("user");
        int i = userService.updatePwd(user.getU_name(), password);
        if (i == 1) {
            mv.setViewName("pages/success");
        } else {
            mv.setViewName("pages/error");
        }
        return mv;
    }

    /**
     * 上传简历
     *
     * @param upload
     * @param request
     * @return
     */
    @RequestMapping("/upload")
    public String upload(MultipartFile upload, HttpServletRequest request) throws IOException {

        //String path = "D:\\ideaproject\\yang_javaee_work\\target\\yang_javaee_work\\images\\resume";
        String sqlpath = request.getSession().getServletContext().getRealPath("/images/resume/");

        File file = new File(sqlpath);
        if (!file.exists()) {
            file.mkdirs();
        }

        String uuid = UUID.randomUUID().toString().replace("-", "");
        String filename = uuid + upload.getOriginalFilename();
        upload.transferTo(new File(sqlpath, filename));

        User user = (User) request.getSession().getAttribute("user");
        userService.upload(user.getU_name(), filename);
        return "pages/success";
    }

    /**
     * 查看个人简历
     *
     * @param request
     */
    @RequestMapping("/findResume")
    public ModelAndView findResume(HttpServletRequest request) {

        ModelAndView mv = new ModelAndView();
        User user = (User) request.getSession().getAttribute("user");
        String name = user.getU_name();
        String resume = userService.findResume(name);
        //String imgPath = "${pageContext.request.contextPath}/images/resume" + resume;
        mv.addObject("resume", resume);

        mv.setViewName("user/find_resume");
        return mv;
    }

    /**
     * 删除个人简历
     *
     * @param request
     */
    @RequestMapping("/deleteResume")
    public ModelAndView deleteResume(HttpServletRequest request) {

        ModelAndView mv = new ModelAndView();
        User user = (User) request.getSession().getAttribute("user");
        String name = user.getU_name();
        int i = userService.delete(name);

        if (i == 1) {
            mv.addObject("deleteResume", "删除数据成功");
        } else {
            mv.addObject("deleteResume", "删除数据失败");
        }
        mv.setViewName("user/delete_resume");
        return mv;
    }

    /**
     * 查看个人投过的招聘
     *
     * @param request
     */
    @RequestMapping("/findAlready")
    public ModelAndView findAlready(HttpServletRequest request) {

        ModelAndView mv = new ModelAndView();
        User user = (User) request.getSession().getAttribute("user");
        String name = user.getU_name();
        List<User> users = userService.findRecruit(name);

        mv.addObject("users", users);

        mv.setViewName("user/find_already");
        return mv;
    }

    /**
     * 投简历
     *
     * @param request
     */
    @RequestMapping("/postResume")
    public ModelAndView postResume(Integer rid, HttpServletRequest request) {

        ModelAndView mv = new ModelAndView();
        User user = (User) request.getSession().getAttribute("user");
        String name = user.getU_name();

        Integer uid = userService.findIdByName(name);
        boolean b = userService.postResume(uid, rid);

        if (b) {
            mv.setViewName("pages/success");
        } else {
            mv.setViewName("pages/error");
        }
        return mv;
    }
}
